package net.lzzy.practices.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import javax.lang.model.type.TypeMirror;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liaodcc.
 */
@Entity
@Getter
@Setter
public class Result extends BaseEntity {
    /**
     * 0 表示正确
     * 1 表示少选
     * 2 表示多选
     * 3 表示错选
     * 4 表示未做
     */
    @Transient
    public static final int TYPE_RIGHT = 0;
    @Transient
    public static final int TYPE_MISS = 1;
    @Transient
    public static final int TYPE_EXTRA = 2;
    @Transient
    public static final int TYPE_WRONG = 3;
    @Transient
    public static final int TYPE_UNFINISHED = 4;
    @Temporal(TemporalType.TIMESTAMP)
    private Date commitTime;

    @ManyToOne
    private User student;
    @ManyToOne
    private Question question;

    @ManyToMany
    private List<Option> options = new ArrayList<>();

    public Result(){
        commitTime  = new Date();
    }
    @JsonIgnore
    public List<Option> getOptions() {
        return options;
    }

    @JsonBackReference
    public Question getQuestion() {
        return question;
    }

    @JsonBackReference
    public User getstudent(){return student;}

    public int what(){
        if (options.size() == 0){
            return TYPE_UNFINISHED;
        }
        boolean miss = false;
        boolean extra = false;
        for (Option option : options){
            if (!option.isAnswer()){
                extra = true;
                break;
            }
        }
        for (Option option : question.getOptions()){
            if (option.isAnswer()){
                if(!options.contains(option)){
                    miss = true;
                    break;
                }
            }
        }
        if(miss && extra){
            return TYPE_WRONG;
        }else if(miss){
            return TYPE_MISS;
        }else if (extra){
            return TYPE_EXTRA;
        }else {
            return TYPE_RIGHT;
        }
    }
}
